﻿using System;
using System.Collections.Generic;
using RabbitMQ.Client;
using smp.RabbitMq.Configuration;

namespace smp.RabbitMq
{
    internal class SingletonFactory
    {
        private static Dictionary<string, ConnectionFactory> _connectionFactories;
        private static IRabbitMqConfiguration _subscribeConfig;
        private static int? _defaultTimeOut;
        private static Dictionary<string, IRabbitMqConfiguration> _publishConnections;
        private static Dictionary<string, IRabbitMqConfiguration> _requestConnections;
        private static readonly Lazy<SingletonFactory> LazyInstance = new Lazy<SingletonFactory>(() => new SingletonFactory());
        private static RabbitMqSection _rabbitMqSection;
        
        private SingletonFactory()
        {
            _connectionFactories = new Dictionary<string, ConnectionFactory>();
            _publishConnections = new Dictionary<string, IRabbitMqConfiguration>();
            _rabbitMqSection = new RabbitMqSection();
        }

        public static SingletonFactory Instance
        {
            get
            {
                return LazyInstance.Value;
            }
        }

        internal ConnectionFactory GetConnectionFactory(IRabbitMqConfiguration configuration)
        {
            var key = configuration.Host + configuration.VirtualHost;
            if (!_connectionFactories.ContainsKey(key))
            {
                _connectionFactories.Add(key, new ConnectionFactory
                {
                    HostName = configuration.Host,
                    VirtualHost = configuration.VirtualHost,
                    UserName = configuration.Username,
                    Password = configuration.Password,
                    Protocol = Protocols.FromEnvironment(),
                    Port = configuration.Port.HasValue ? configuration.Port.Value : AmqpTcpEndpoint.UseDefaultPort
                });
            }
            return _connectionFactories[key];
        }

        public IRabbitMqConfiguration GetSubscriberConfiguration()
        {
            if(_subscribeConfig == null)
            {
                _subscribeConfig = _rabbitMqSection.GetSubscriber();
            }
            return _subscribeConfig;
        }

        public int DefaultTimeOut
        {
            get
            {
                if(!_defaultTimeOut.HasValue)
                {
                    _defaultTimeOut = _rabbitMqSection.GetDefaultTimeOut();
                }
                return _defaultTimeOut.Value;
            }
        }

        public IRabbitMqConfiguration GetPublisherConfiguration(string key)
        {
            if(_publishConnections.ContainsKey(key))
            {
                _publishConnections.Add(key, _rabbitMqSection.GetPublisher(key));
            }
            return _publishConnections[key];
        }

        public IRabbitMqConfiguration GetRequesterConfiguration(string key)
        {
            if (_requestConnections.ContainsKey(key))
            {
                _requestConnections.Add(key, _rabbitMqSection.GetRequester(key));
            }
            return _requestConnections[key];
        }
    }
}
